package com.itheima.Day0610.Demo02.Service;

import com.itheima.Day0610.Demo02.Dao.StudentDao;
import com.itheima.Day0610.Demo02.Domain.Student;

import java.util.ArrayList;

public class StudentService {
    StudentDao sd = new StudentDao();

    public boolean checkId(String id) {
        ArrayList<Student> list = sd.findStudent();
        for (int i = 0; i < list.size(); i++) {
            Student stu = list.get(i);
            if (stu.getId().equals(id)) {
                return false;
            }
        }
        return true;
    }

    public void addStudent(Student s) {
        sd.addStudent(s);
    }

    public ArrayList<Student> findStudent() {
        ArrayList<Student> list = sd.findStudent();
        return list;
    }

    public boolean deleteStudent(String id) {
        ArrayList<Student> list = sd.findStudent();
        for (int i = 0; i < list.size(); i++) {
            Student stu = list.get(i);
            if (stu.getId().equals(id)) {
                sd.deleteStudent(i);
                return true;
            }
        }return false;
    }

    public void updateStudent(Student s) {
        ArrayList<Student> list = sd.findStudent();
        for (int j = 0; j <list.size() ; j++) {
            Student stu = list.get(j);
            if (stu.getId().equals(s.getId())) {
                sd.updateStudent(j,s);
            }
        }
    }
}
